/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SendMail.java
 *
 * Created on 10-06-2011, 06:12:09
 */
package Presentation;

import diaoc.SendMailOK;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Nixforest
 */
public class SendMail extends javax.swing.JInternalFrame {

    /** Creates new form SendMail */
    public SendMail(ArrayList<String> listEmail) {
        initComponents();
        Creation();
        ListEmail = (ArrayList<String>)listEmail.clone();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtAreaDen = new javax.swing.JTextArea();
        tbxChuDe = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtAreaNoiDung = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        btnGui = new javax.swing.JButton();
        btnHuy = new javax.swing.JButton();

        setClosable(true);
        setTitle(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("titlesendmail"));
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jLabel1.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("to"));

        txtAreaDen.setColumns(20);
        txtAreaDen.setLineWrap(true);
        txtAreaDen.setRows(5);
        txtAreaDen.setWrapStyleWord(true);
        txtAreaDen.setAutoscrolls(false);
        jScrollPane1.setViewportView(txtAreaDen);

        jLabel2.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("subject"));

        txtAreaNoiDung.setColumns(20);
        txtAreaNoiDung.setLineWrap(true);
        txtAreaNoiDung.setRows(5);
        txtAreaNoiDung.setWrapStyleWord(true);
        txtAreaNoiDung.setAutoscrolls(false);
        jScrollPane2.setViewportView(txtAreaNoiDung);

        jLabel3.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("body"));

        btnGui.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("send"));
        btnGui.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuiActionPerformed(evt);
            }
        });

        btnHuy.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("close"));
        btnHuy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHuyActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tbxChuDe, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnGui)
                        .addGap(18, 18, 18)
                        .addComponent(btnHuy)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tbxChuDe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnGui)
                    .addComponent(btnHuy))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        String to = "";
        for(String email : ListEmail){
            to += email + ";";
        }
        txtAreaDen.setText(to);
    }//GEN-LAST:event_formInternalFrameOpened

    private void btnGuiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuiActionPerformed
        String chuDe = tbxChuDe.getText().trim();
        String noiDung = txtAreaNoiDung.getText().trim();
        if (chuDe.isEmpty()) {
            JOptionPane.showMessageDialog(rootPane,
                    java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("not enter subject"));
        }else if(noiDung.isEmpty()){
            JOptionPane.showMessageDialog(rootPane,
                    java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("not enter body"));
        }else{
            for(String email : ListEmail){
                try {
                    SendMailOK.send(SendMailOK.Server, email, SendMailOK.companyMail,
                            SendMailOK.companyMailPass, chuDe, noiDung);
                } catch (Exception ex) {
                    Logger.getLogger(SendMail.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            JOptionPane.showMessageDialog(rootPane,
                    java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("sended"));
            this.dispose();
        }
    }//GEN-LAST:event_btnGuiActionPerformed

    private void btnHuyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHuyActionPerformed
        if (JOptionPane.showConfirmDialog(rootPane,
                java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("really delete mail"), java.util.ResourceBundle.getBundle("Presentation/vn").getString("warn"), JOptionPane.YES_NO_OPTION) == 0) {
            this.dispose();
        }else return;
    }//GEN-LAST:event_btnHuyActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnGui;
    private javax.swing.JButton btnHuy;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField tbxChuDe;
    private javax.swing.JTextArea txtAreaDen;
    private javax.swing.JTextArea txtAreaNoiDung;
    // End of variables declaration//GEN-END:variables
    private ArrayList<String> ListEmail;
    private void Creation(){
        ListEmail = new ArrayList<String>();
    }
}
